package Dao;

import domain.news;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import uilts.C3P0utils;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class NewsDao {
    C3P0utils c3P0utils=new C3P0utils();
    /*
    * 查询出新闻标题
    * */

    public List<news> findnewAll(String sql){
        QueryRunner queryRunner = new QueryRunner(c3P0utils.getDataSource());
        try {
            List<news> query = queryRunner.query(sql, new BeanListHandler<news>(news.class));
            return  query;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }
/*删除新闻*/
    public boolean delete(String sql, String id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(c3P0utils.getDataSource());
        int i = queryRunner.update(sql, id);
        boolean flag = false;
        if (i > 0) {
            flag = true;
        }
        return flag;
    }

    /*新增新闻*/
    public boolean addnews(String sql, String title, String body) {
        QueryRunner queryRunner = new QueryRunner(c3P0utils.getDataSource());
        try {
            int update = queryRunner.update(sql, title, body, new Date());

            if (update > 0) {
                return true;
            } else {
                return false;
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
/*模糊查询*/
    public List<news> cx(String sql, String title) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(c3P0utils.getDataSource());
        List<news> query = queryRunner.query(sql, new BeanListHandler<news>(news.class), title);
        return query;
    }

    public boolean xg(String sql, String title, String body,String id) {
        QueryRunner queryRunner = new QueryRunner(c3P0utils.getDataSource());
        try {
            int update = queryRunner.update(sql, title, body);
            if (update > 0) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
